package com.onpositive.data.kvstorage;

import com.onpositive.data.IPropertyMetadata;
import com.onpositive.datamap.core.ITextSection;
import com.onpositive.datamap.core.impl.DataCollection;
import com.onpositive.datamap.core.impl.DataObject;
import com.onpositive.datamap.item.SingleDataObject;

public class KeyValueDataCollection extends DataCollection{

	public KeyValueDataCollection(IKeyValueAccess access) {
		super(new KeyValueBasedCollection(access), null,null);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected DataObject createDataObject(int num) {
		return new DataObject(this, num){
			
			@Override
			public String getTextContentAsString() {
				KeyValueBasedCollection m= (KeyValueBasedCollection) getCollection();
				SingleDataObject object = m.getObject(num);
				return object.getTextContentAsString();
			}
			@Override
			public ITextSection getTextDescription() {
				KeyValueBasedCollection m= (KeyValueBasedCollection) getCollection();
				SingleDataObject object = m.getObject(num);
				return object.getTextDescription();
			}
		};
	}
	
	protected void onPropertyGroupsChanged() {		
		super.onPropertyGroupsChanged();
		KeyValueBasedCollection m= (KeyValueBasedCollection) getCollection();
		m.store();
	}
	
	@Override
	protected void updateMeta(String id) {
		onPropertyGroupsChanged();
	}
	
	public SingleDataObject getSingleDataObject(int num){
		KeyValueBasedCollection m= (KeyValueBasedCollection) getCollection();
		return m.getObject(num);
	}

	public SingleDataObject getSingleDataObject(String id) {
		KeyValueBasedCollection m= (KeyValueBasedCollection) getCollection();
		return m.getObject(id);
	}

	public IPropertyMetadata[] getMeta() {
		return getCollection().getAllMetas();
		
	}
}
